package com.weayer.service;

import com.weayer.constant.CommonStatusEnum;
import com.weayer.constant.DriverConstants;
import com.weayer.dto.DriverCarBindingRelationship;
import com.weayer.dto.DriverUserWorkStatus;
import com.weayer.dto.ResponseResult;
import com.weayer.remote.ServiceDriverClient;
import com.weayer.remote.ServiceOrderClient;
import com.weayer.request.DriverGrabRequest;
import com.weayer.response.OrderDriverResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @description: Api服务 - 司机 - 订单service
 * @author: Weayer
 **/
@Service
@Slf4j
public class ApiDriverOrderInfoService {
    @Autowired
    ServiceOrderClient serviceOrderClient;

    @Autowired
    ServiceDriverClient serviceDriverClient;

    // 司机抢单
    public ResponseResult grab(String driverPhone, Long orderId, String receiveOrderCarLongitude, String receiveOrderCarLatitude){
        // 1. 根据 driverPhone ，查询绑定的车辆
        ResponseResult<DriverCarBindingRelationship> driverCarRelationShipResponseResult = serviceDriverClient.getDriverCarRelationShip(driverPhone);

        if (driverCarRelationShipResponseResult == null){
            return ResponseResult.fail(CommonStatusEnum.DRIVER_CAR_BIND_NOT_EXISTS.getCode(),CommonStatusEnum.DRIVER_CAR_BIND_EXISTS.getValue());
        }
        DriverCarBindingRelationship driverCarBindingRelationship = driverCarRelationShipResponseResult.getData();
        Long carId = driverCarBindingRelationship.getCarId();
        // 2. 根据 carId ，查询司机是否可以正常工作
        ResponseResult<OrderDriverResponse> availableDriverResponseResult = serviceDriverClient.getAvailableDriver(carId);
        if (availableDriverResponseResult == null){
            return ResponseResult.fail(CommonStatusEnum.AVAILABLE_DRIVER_EMPTY.getCode(),CommonStatusEnum.AVAILABLE_DRIVER_EMPTY.getValue());
        }
        OrderDriverResponse orderDriverResponse = availableDriverResponseResult.getData();
        Long driverId = orderDriverResponse.getDriverId();
        String licenseId = orderDriverResponse.getLicenseId();
        String vehicleNo = orderDriverResponse.getVehicleNo();
        String vehicleType = orderDriverResponse.getVehicleType();

//        orderInfo.setOrderStatus(OrderConstants.DRIVER_RECEIVE_ORDER);

        // 3. 执行抢单动作
        DriverGrabRequest driverGrabRequest = new DriverGrabRequest();
        driverGrabRequest.setOrderId(orderId);
        driverGrabRequest.setDriverId(driverId);
        driverGrabRequest.setCarId(carId);
        driverGrabRequest.setDriverPhone(driverPhone);
        driverGrabRequest.setLicenseId(licenseId);
        driverGrabRequest.setVehicleNo(vehicleNo);
        driverGrabRequest.setVehicleType(vehicleType);
        driverGrabRequest.setReceiveOrderCarLatitude(receiveOrderCarLatitude);
        driverGrabRequest.setReceiveOrderCarLongitude(receiveOrderCarLongitude);
        ResponseResult responseResult = serviceOrderClient.driverGrab(driverGrabRequest);

        if (responseResult.getCode() != CommonStatusEnum.SUCCESS.getCode()){
            return ResponseResult.fail(CommonStatusEnum.ORDER_UPDATE_ERROR.getCode(),CommonStatusEnum.ORDER_UPDATE_ERROR.getValue());
        }
        // 4. 修改司机的工作状态
        DriverUserWorkStatus driverUserWorkStatus = new DriverUserWorkStatus();
        driverUserWorkStatus.setDriverId(driverId);
        driverUserWorkStatus.setWorkStatus(DriverConstants.DRIVER_WORK_STATUS_SERVING);

        responseResult = serviceDriverClient.changeWorkStatus(driverUserWorkStatus);
        if (responseResult.getCode() != CommonStatusEnum.SUCCESS.getCode()){
            return ResponseResult.fail(CommonStatusEnum.DRIVER_STATUS_UPDATE_ERROR.getCode(),CommonStatusEnum.DRIVER_STATUS_UPDATE_ERROR.getValue());
        }

        return responseResult.success();
    }
}
